package util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Properties;

import core.obj.Invoice;
import core.obj.Paycheck;
import core.obj.Payment;
import core.obj.Resume;
import core.obj.StaffingReq;
import core.obj.TimeReport;
import core.sub.Company;
import core.sub.Contractor;
import core.sub.ContractorAsEmployee;
import core.sub.Employee;
import core.sub.Subject;

public class CommonUtils {

	public static File getPropertiesFile(String resourceName, ClassLoader classLoader) {
	    if (classLoader == null)
            classLoader = CommonUtils.class.getClassLoader();

        URL resource = classLoader.getResource("resources/"+resourceName);
        if (resource == null) {
            File file = new File(resourceName);
            if (file.exists()) {
                return file;
            }
            else {
                file = new File(System.getProperty("user.home") + File.separator + resourceName);
                if (file.exists()) {
                    return file;
                }
                else
                    throw new RuntimeException("Cannot find resource property file called " + resourceName + ".");
            }
        }
        
        try {
			return new File(resource.toURI());
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
        return null;
	}

    public static Properties loadPropertiesFile(String resourceName, ClassLoader classLoader) {
        InputStream is = null;
        try {
            if (classLoader == null)
                classLoader = CommonUtils.class.getClassLoader();

            URL resource = classLoader.getResource("resources/"+resourceName);
            if (resource == null) {
                File file = new File(resourceName);
                if (file.exists())
                    resource = file.toURI().toURL();
                else {
                    file = new File(System.getProperty("user.home") + File.separator + resourceName);
                    if (file.exists())
                        resource = file.toURI().toURL();
                    else
                        throw new RuntimeException("Cannot find resource property file called " + resourceName + ".");
                }
            }

            is = resource.openStream();
            Properties properties = new Properties();
            properties.load(is);

            return properties;
        } catch (IOException e) {
            throw new RuntimeException("Cannot load resource property file.", e);
        } finally {
            if (is != null)
                try {
                    is.close();
                } catch (IOException e) {}
        }
    }
    
    public static int getSubjectType(Subject sub) {
    		int type = -1;
    		if(sub instanceof Company)
    			type = Constants.COMPANY_SUB;
    		else if(sub instanceof Contractor)
    			type = Constants.CONTRACTOR_SUB;
    		else if(sub instanceof Employee)
    			type = Constants.EMPLOYEE_SUB;
    		else if(sub instanceof ContractorAsEmployee)
    			type = Constants.EMPLOYEE_CONTRACTOR_SUB;
    		return type;
    }
    
    public static int getObjectType(Object obj) {
		int type = -1;
		if(obj instanceof Invoice)
			type = Constants.INVOICE_OBJ;
		else if(obj instanceof Paycheck)
			type = Constants.PAYCHECK_OBJ;
		else if(obj instanceof Payment)
			type = Constants.PAYMENT_OBJ;
		else if(obj instanceof Resume)
			type = Constants.RESUME_OBJ;
		else if(obj instanceof StaffingReq)
			type = Constants.STAFF_REQ_OBJ;
		else if(obj instanceof TimeReport)
			type = Constants.TIME_REPORT_OBJ;
		return type;
}    
}
